<?php
abstract class Main_Pager
{
	/**
	 * @var int
	 */
	protected $_langId;
	
	/**
	 * @var Zend_Paginator
	 */
	protected $_paginator;
	
	/**
	 * @var Main_Db_Adapter_Abstract
	 */
	protected $_db;
	
	/**
	 * @var array
	 */
	protected $_config;
	
	/**
	 * @var string
	 */
	protected $_name;
	
	/**
	 * @var array
	 */
	protected $_params;
	
	/**
	 * @var array
	 */
	protected $_options;

	/**
	 * @var array
	 */
	protected $_fields;
	
	/**
	 * @var object
	 */
	protected $_service;
	
	/**
	 * @param string $name
	 * @param array $options
	 * @return void
	 */
	public function __construct($name, $options = array())
	{
		$this->_name = $name;
		$this->_options = $options;
		
		$this->_langId = Zend_Registry::getInstance()->langId;
		
		$this->_db = Main_Db::get();
		
		$this->_init();	
	}

	/**
	 * @param string $name
	 * @param string $uid
	 * @param array $options
	 * @return Main_Pager
	 */
	public static function factory($name, $uid, Array $options = array()) 
	{
		$className = Main_Const::APP_NAMESPACE . $name;
		if (! class_exists($className)) {
			throw new Exception("Класс $className не существует");
		} else {
			return new $className($uid, $options);
		}  
	}
	
	/**
	 * @return void
	 */
	protected function _init() 
	{
	}

	/**
	 * @param array $request
	 * @return Main_Pager
	 */
	protected function _setParams($request) 
	{
		$this->_params = $request;
		return $this;
	}
	
	/**
	 * @return string
	 */
	public function load()
	{
		$this->_paginator->setItemCountPerPage($this->_params["rows"]);
		$this->_paginator->setCurrentPageNumber($this->_params["page"]);
			
		$response = (array) $this->_paginator->getPages();
		$currentItems = $this->_paginator->getCurrentItems();
		$response += array('row' => $currentItems);
		
		return $response;
	}
	
	/**
	 * @return void
	 */
	protected function _defineOrderBySql()
	{
		if ( !empty($this->_params['sidx']) 
		    && is_array($this->_params['sidx'])
		    && !empty($this->_params['sord']) 
		) {
			$orderBy = implode(',', $this->_params['sidx']);
			$sortorder = (in_array(strtolower($this->_params['sord']), array('asc','desc'))) ?
						' ' . $this->_params['sord'] : '';
			$this->_config['orderBy'] = ' ORDER BY ' . $orderBy . $sortorder;
		}	  
	}
	
	/**
	 * @return void
	 */
	protected function _paginatorInit() 
	{
		$where = (isset($this->_config['where'])) ? $this->_config['where'] : ''; 
		
		$this->_config['sql'] = $this->_config['base'] . $where . $this->_config['orderBy'];
		
		//file_put_contents('ff123', $this->_config['sql']);
		
		$this->_config['count'] .= $where;
		
		$adapter = new Main_Paginator_Adapter_Db($this->_db,
												 $this->_config['sql'],
												 $this->_config['count']);
		$this->_paginator = new Zend_Paginator($adapter);
	}
	
	/**
	 * @param string $searchString
	 * @return void
	 */
	protected function _simpleSearchSql($searchString)
	{
		if (is_array($this->_fields) && (trim($searchString) != '')) {
			$addition = ''; $sql = '';
			foreach ($this->_fields as $filedName => $filedProperties) {
				if (isset($filedProperties['simpleSearch']) && $filedProperties['simpleSearch']) {
					$sql .= $addition . " (UPPER({$filedProperties['sql']}) LIKE UPPER('%{$searchString}%')) ";
					$addition = ' OR';
				} 
			}
			if ($sql != '') {
				$sql = ' AND (' . $sql . ')';
				$this->_config['where'] = $sql;
			}
		}
	}

	/**
	 * @param array $request
	 * @return string|bool 
	 */
	public function getResponse($request) 
	{
		$this->_setParams($request);
		
		$isLoad = false;
		if ($this->_params['ac'] == 'simple_search') {
			$this->_simpleSearchSql($this->_params['search_string']);					
			$isLoad = true;
		}
		
		$this->_defineOrderBySql();
		
		if ( ($this->_params['ac'] == 'load') || $isLoad){
			$this->_paginatorInit(); 
			return $this->load();
		}
		if ($this->_params['ac'] == 'delete_row') {
			if ($this->_deleteItem($this->_params['primary'])) {
				$this->_paginatorInit();
				return $this->load();	
			} 
		}
		if ($this->_params['ac'] == 'set_current') {
			$this->_paginatorInit();
			if ($this->_paginator->setCurrentPageNumber($this->_params["page"])) {
				return $this->load();
			}
		}
		if ($this->_params['ac'] == 'set_item_count_per_page') {
			$this->_paginatorInit();
			if ($this->_paginator->setItemCountPerPage($this->_params["value"])) {
				return $this->load();
			}
		}
		
		return false;
	}
	
	/**
	 * @param string|int $primary
	 * @return bool
	 */
	protected function _deleteItem($primary) 
	{
	}
		
	
	
	
	
}
